""" AdminMenu Model """

from masoniteorm.models import Model
import datetime
from masoniteorm.collection import Collection


class AdminMenu(Model):
    """AdminMenu Model"""
    FIELDS = {
        'is_show': {0: '否', 1: '是'}
    }

    @classmethod
    def generate_log(cls, pid, rank, title, uri, is_show):
        """
        生成日志
        @param pid:
        @param rank:
        @param title:
        @param uri:
        @param is_show:
        @return:
        """
        created_at = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        cls.create({
            'pid': pid,
            'rank': rank,
            'title': title,
            'uri': uri,
            'is_show': is_show,
            'created_at': created_at,
            'updated_at': created_at
        })

    @classmethod
    def get_title(cls):
        """
        获取表中所有的 title
        {1: '系统设置', 2: '菜单', 3: '系统参数', 4: '管理员', 5: '角色', 6: '操作日志', 7: '登录日志', 8: '访问权限'}
        @return dict
        """
        titles = Collection(cls.get()).pluck('title', 'id').all()
        titles[0] = '-'
        return titles

    @classmethod
    def get_all_pid(cls):
        """
        获取访问权限的 title
        @return [{'id': 1, 'title': '系统设置'}, {'id': 2, 'title': '菜单'}]
        """
        menus = cls.where('pid', 0).where('is_show', 1).order_by('rank', 'DESC').get()
        data = []
        if menus is None:
            return data

        for menu in menus:
            data.append({'id': menu.id, 'title': menu.title})
        return data
